package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import com.xuecheng.manage_cms.web.controller.CmsPageController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author: Han_xu
 * @Date: 2018/11/16 下午 03:22
 * @Version 1.0
 */
@Service
public class PageService {

    @Autowired
    CmsPageRepository cmsPageRepository;

    /**
     * 分页查询
     * @param page  当前页数
     * @param size  每页显示的记录数
     * @param queryPageRequest  请求的条件
     * @return
     */
    public QueryResponseResult findList(int page, int size, QueryPageRequest queryPageRequest) {
        //1、接收参数，并进行校验
        if (page <=0){
            page =1;
        }
        page = page -1;//page默认从0开始，为了与前端的操作
        if (size <=0){
            size =10;
        }

        if (queryPageRequest == null){
            queryPageRequest = new QueryPageRequest();
        }

        //2、执行方法查询
        Pageable pageable = PageRequest.of(page,size);//封装分页条件

        Page <CmsPage> cmsPages = cmsPageRepository.findAll(pageable);
        //3、封装结果集合
        QueryResult queryResult = new QueryResult();
        queryResult.setList(cmsPages.getContent());
        queryResult.setTotal(cmsPages.getTotalElements());
        QueryResponseResult result = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return result;
    }

}
